package edu.kaifadui.lmbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.kaifadui.lmbase.pojo.Student;
import edu.kaifadui.lmbase.mapper.StudentMapper;
import edu.kaifadui.lmbase.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.kaifadui.lmbase.utils.JsonUtil;
import edu.kaifadui.lmbase.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈世杰
 * @since 2022-02-22
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Result deleteStudent(IdCode idCode) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no",idCode.getNo());
        if (studentMapper.deleteByMap(map) > 0){
            return new Result(200,idCode,"删除成功");
        }
        return new Result(0,idCode,"删除失败");
    }

    @Override
    public Result getStudentAllInfoById(IdCode idCode) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no",idCode.getNo());
        List<Student> students = studentMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(students)){
            return new Result(0,idCode,"查询失败");
        }
        Student student = students.get(0);
        student.setPassword("保密");
        return new Result(200,student,"查询成功");
    }

    @Override
    public Result getStudentContactWay(IdCode idCode) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no",idCode.getNo());
        List<Student> students = studentMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(students)){
            return new Result(0,idCode,"查无此人");
        }
        ObjectNode emptyJson = JsonUtil.getEmptyJson();
        emptyJson.put("tel",students.get(0).getPhone());
        return new Result(200,emptyJson,"获取联系方式成功");
    }

    @Override
    public Result checkIsUser(Login login) {
        Map<String,Object> map = new HashMap<>(1);
        map.put("no",login.getNo());
        List<Student> students = studentMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(students)){
            return new Result(0,"查无此人");
        }
        String check = DigestUtils.md5DigestAsHex((login.getPassword() + students.get(0).getCreatetime()).getBytes(StandardCharsets.UTF_8));
        String password = students.get(0).getPassword();
        if (password.equals(check)){
            Student student = students.get(0);
            student.setPassword("保密");
            return new Result(200,student,"登录成功");
        }
        return new Result(0,"密码错误");
    }

    @Override
    public Result bandEmail(BandEmail bandEmail) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no",bandEmail.getNo());
        List<Student> students = studentMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(students)){
            return new Result(0,"查无此人");
        }
        Student student = students.get(0);
        student.setNote1(bandEmail.getEmail());
        if (studentMapper.updateById(student) > 0){
            student.setPassword("保密");
            return new Result(200,student,"绑定成功");
        }
        return new Result(0,"绑定失败");
    }

    @Override
    public Result modifyPassword(Login login) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no",login.getNo());
        List<Student> students = studentMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(students)){
            return new Result(0,"查无此人");
        }
        Student student = students.get(0);
        student.setPassword(DigestUtils.md5DigestAsHex((login.getPassword() + student.getCreatetime()).getBytes(StandardCharsets.UTF_8)));
        if (studentMapper.updateById(student) > 0){
            student.setPassword(null);
            return new Result(200,student,"成功修改密码");
        }
        return new Result(0,"修改密码失败");
    }

    @Override
    public Result modifyGender(ChangeGender gender) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no",gender.getNo());
        List<Student> students = studentMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(students)){
            return new Result(0,"查无此人");
        }
        Student student = students.get(0);
        student.setNote2(gender.getGender());
        if (studentMapper.updateById(student)>0){
            student.setPassword("保密");
            return new Result(200,student,"性别选择成功");
        }
        return new Result(0,"选择性别失败");
    }
}
